import $ from '../common';
Page({
  data: {
    titleHeight: $.gData().titleHeight,
    statuHeight: $.gData().statuHeight,
    isIphoneX: $.gData().isIphoneX,
    _components_modal_hide: true,
    info: {},
    defimg: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Finews.gtimg.com%2Fnewsapp_bt%2F0%2F14087354835%2F1000.jpg&refer=http%3A%2F%2Finews.gtimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1651388881&t=8d3322dd433dcde1339e81bf8f029364'
  },
  afterRead(event) {//上传
    const { file } = event.detail;
    const { info } = this.data
    let files = file.map(v => {//因为vant 返回的解构是[{url:'............'}] $.uploadFileList 需要的结构式[1,2,3]
      return v.url
    })
    $.uploadFileList("/image/upload/oss", {
      prefix: "keday/",
      fileList: files
    }, res => {
      let a = res.map(i => {//返回的结构式[1,2,3,5] 需要改成vant需要的[{url:'............'},{}]
        return {
          url: i
        }
      })
      let arr  //判断之前有没有 图片 有的话合并
      if(info.imgs){
       arr= [...info.imgs, ...a]
      }else{
       arr= [ ...a]
      }
      this.setData({
        'info.imgs': arr
      })
    })
  },
  imgdelete(event){//删除图片
    const { info}=this.data
    info.imgs.splice(event.detail.index,1)
    this.setData({
      info
    })
  },
  getinfo() { //获取用户信息
    const {
      student_uuid,
      campus_uuid,
      growingstudent_uuid
    } = this.data
    $.get('/student/growing/detail', {
      student_uuid,
      campus_uuid,
      growingstudent_uuid
    }, res => {
      this.setData({
        info: res
      })
      if (res.imgs) {
        let a = res.imgs.map(i => {
          return {
            url: i
          }
        })
        this.setData({
          'info.imgs': a
        })
      }
    })
  },
  savehand() { //保存
    const { info,  growingstudent_uuid,  student_uuid, growing_uuid } = this.data
    if (!info.content) {
      $.tip('点评内容不能为空')
      return
    }
    if (info.imgs) {
      let a = info.imgs.map((v, i) => {//保存的时候需要将 [{url:'............'},{}] 改成[1,2,3]
        return info[i] = v.url
      });
      info.imgs = a
    }
    let data = {
      content: info.content,
      imgs: info.imgs || [],
    }
    let url
    if (growingstudent_uuid) {
      data.growingstudent_uuid = growingstudent_uuid
      url = '/student/growing/update'
    } else {
      data.student_uuid = student_uuid,
      data.growing_uuid = growing_uuid,
      url = '/student/growing/create'
    }
    $.post(url, data, res => {
      wx.navigateBack({
        delta: -1,
      })
    })
  },
  delehand() { //删除点击打开模态框
    this.setData({
      _components_modal_hide: false
    })
  },
  cancel() { //取消删除
    this.setData({
      _components_modal_hide: true
    })
  },
  determine(e) { //确定删除
    this.cancel()
    this.deleuser()
  },
  textinput(e) { //text输入
    this.setData({
      'info.content': e.detail.value
    })
  },
  deleuser() { //删除的请求
    const {
      growingstudent_uuid
    } = this.data
    $.get('/student/growing/remove', {
      growingstudent_uuid
    }, res => {
      console.log('删除成功');
      wx.navigateBack({
        delta: -1,
      })
    })
  },
  onLoad(e) {
    const {
      student_uuid,
      campus_uuid,
      growingstudent_uuid,
      growing_uuid
    } = e
    this.setData({
      student_uuid,
      campus_uuid,
      growingstudent_uuid,
      growing_uuid
    })
    this.getinfo()
  },
  onShow() {},
})